using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Volo.Abp.AspNetCore.SignalR;
using Volo.Abp.DependencyInjection;

namespace Acme.BookStore.Hubs
{
    /// <summary>
    /// 大屏数据实时推送Hub
    /// </summary>
    [HubRoute("/dashboardHub")]
    public class DashboardHub : AbpHub
    {
        /// <summary>
        /// 向所有客户端推送库存主数据
        /// </summary>
        public async Task SendStockMainUpdate(object data)
        {
            await Clients.All.SendAsync("ReceiveStockMainUpdate", data);
        }

        /// <summary>
        /// 客户端请求初始库存数据
        /// </summary>
        public async Task RequestStockMainData()
        {
            // 该方法会触发后端服务获取最新库存数据并推送
            await Clients.Caller.SendAsync("RequestStockMainData");
        }

        /// <summary>
        /// 客户端订阅库存数据更新
        /// </summary>
        public async Task SubscribeToStockUpdates()
        {
            await Groups.AddToGroupAsync(Context.ConnectionId, "StockDataSubscribers");
            await Clients.Caller.SendAsync("SubscriptionStatus", true, "已订阅库存数据更新");
        }

        /// <summary>
        /// 客户端取消订阅库存数据更新
        /// </summary>
        public async Task UnsubscribeFromStockUpdates()
        {
            await Groups.RemoveFromGroupAsync(Context.ConnectionId, "StockDataSubscribers");
            await Clients.Caller.SendAsync("SubscriptionStatus", false, "已取消订阅库存数据更新");
        }
    }
} 